let filePath2 = []
let urlarr = [
  "https://6a69-jia-2004-7gy5ewbi2ec368cd-1323896692.tcb.qcloud.la/weda-uploader/1a2bd162271aa2c8eb14a0f62d875471-非会员.png?sign=cad33c84a36bda33810b6e2d887b3e55&t=1730098900",
  "cloud://jia-2004-7gy5ewbi2ec368cd.6a69-jia-2004-7gy5ewbi2ec368cd-1323896692/weda-uploader/2fc54244d0438913a7d8e659be156c8d-会员.png"
]
const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlarr2:[],
    params:{
      moodTitle:"",
      userId:"",
      moodDesc:"",
      moodPicture:"",
      fileList:[]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  getdate() {
    // const date1 = new Date('2008-08-08 08:08')
    const date1 = new Date()
    // console.log(date1)
    // console.log(date1.getMonth() + 1)
    const month =
      date1.getMonth() + 1 >= 10
        ? date1.getMonth() + 1
        : '0' + (date1.getMonth() + 1) + '-'
    const day =
      date1.getDate() >= 10
        ? date1.getDate() + ' '
        : '0' + date1.getDate() + ' '
    const hour =
      date1.getHours() >= 10 ? date1.getHours() : '0' + date1.getHours()
    const min =
      date1.getMinutes() >= 10
        ? date1.getMinutes()
        : '0' + date1.getMinutes()
    const second =
      date1.getSeconds() >= 10
        ? date1.getSeconds()
        : '0' + date1.getSeconds()
    return `${date1.getFullYear()}-${month}${day}${hour}:${min}:${second}`
  },

  onLoad(options) {
   const allid=wx.getStorageSync('id') 
    this.setData({
      id:options.id||allid||"vinSur9aoaEQydwf"
    })
    this.getdata()
  },
  getdata(){
    db.collection("student").doc(this.data.id).get().then(res=>{
      this.setData({  
        situation:urlarr[res.data.situation],
        avater:res.data.avater,
        name:res.data.name
      })
    })
  },
  updataInputVal(e){
    console.log(e);
    this.setData({
      title:e.detail
    })
  },
  updataInputVal1(e){
    console.log(e);
    this.setData({
      content:e.detail
    })
  },
  readFile(event){
    // const file = event.detail.file 
    // console.log("文件",file)
    console.log(event);
    let filePath = event.detail.file.tempFilePath
        this.cloudFile(filePath)
        // console.log(res);
  },
    // 上传单张图片
  cloudFile(Path) {
    wx.cloud.uploadFile({
      cloudPath: Date.now() + ".jpg",
      filePath: Path
    }).then(res => {
      console.log(res);
      const { fileList = [] } = this.data;
      
      const pic=this.cloudId2Http(res.fileID)
      fileList.push({ url: pic });
      console.log(pic);
      this.setData({
        picurl:pic,
        fileList:fileList
      })
    })
  },
  del(e){
  this.setData({
    fileList:[]
  })
  },
cloudId2Http(picurl, styleName){
    if (!picurl) return "请传入cloudID";
    const strs = picurl.split(".");
    const strs_2 = strs[1];
    const strs_3 = strs_2.split("/");
    const envId = strs_3[0];
    const https_prefix = `https://${envId}.tcb.qcloud.la/`;
    let filePath = "";
    for (let i = 1; i < strs_3.length; i++) {
      if (i === strs_3.length - 1) {
        const fileName = strs_3[i] + "." + strs[strs.length - 1];
        filePath += fileName;
      } else {
        filePath += strs_3[i] + "/";
      }
    }
    let httpsUrl = https_prefix + filePath;
    if (styleName) httpsUrl += "/" + styleName;
    return httpsUrl;
},
submitMood(){
  const time=this.getdate()
  db.collection("pyq").add({
    data:{
      avater:this.data.avater,
      name:this.data.name,
      time:time,
      situation:this.data.situation,
      title:this.data.title,
      content:this.data.content,
      pic:this.data.picurl
    }
  }).then(res=>{
    console.log(res);
    wx.showToast({
      title: '发布成功',
    })
    setTimeout(function(){
      wx.navigateBack()
    },1000)
  })
},
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})